// pages/user/order/evaluate/components/evaluate.js
Component({
	behaviors: ['wx://form-field'],

	options: {
		styleIsolation: 'apply-shared'
	},

	/**
	 * 组件的属性列表
	 */
	properties: {
		goods: Object
	},

	/**
	 * 组件的初始数据
	 */
	data: {
		maxImageCount: 6,
		imageList: [],
	},

	/**
	 * 组件的方法列表
	 */
	methods: {
		initValue: function() {
			if (this.data.value) {
				return;
			}

			const value = {
				order_goods_id: this.data.goods.order_goods_id,
				goods_id: this.data.goods.goods_id,
				content: '',
				uploaded: []
			};
			this.data.value = value;
			this.setData({
				value: value
			});
		},

		// 获取输入内容
		bindInputValue: function(e) {
			this.initValue();
			const value = this.data.value;
			value.content = e.detail.value;
			this.setData({
				value: value,
			});
		},

		// 多图片上传
		chooseImageTap: function(e) {
			this.initValue();
			const globalImageList = this.data.imageList;
			const currentImageCount = globalImageList.length;
			wx.promise.chooseImage({
				count: this.data.maxImageCount - currentImageCount,
			}).then((res) => {
				// 返回选定照片的本地文件路径列表，tempFilePath可以作为img标签的src属性显示图片
				const waitFiles = res.tempFilePaths;
				let saveImageList = JSON.parse(JSON.stringify(globalImageList));
				// 上传图片
				wx.uploader(waitFiles, {
					name: 'iFile',
					onUploadChange: (res) => {
						saveImageList = saveImageList.concat([res.file_path]);
						this.data.value.uploaded.push(res.file_id);
						this.setData({
							value: this.data.value,
							imageList: saveImageList
						}, () => {
							this.data.imageList = globalImageList.concat(waitFiles);
						});
					}
				});
			});
		},

		// 删除图片
		imgItemDel: function(e) {
			const index = e.currentTarget.dataset.index;
			const imageList = this.data.imageList;
			imageList.splice(index, 1);
			this.setData({
				imageList: imageList
			})
		},

		// 点击图片预览
		showImgTap: function(e) {
			const imageList = this.data.imageList;
			const index = e.currentTarget.dataset.index;
			wx.previewImage({
				urls: imageList,
				current: imageList[index],
			})
		},
	}
});
